package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.enums.LindiContractTemplateFieldOptionEnum;
import com.minimalist.common.entity.lindi.po.LindiContractTemplateFieldOption;
import com.minimalist.common.entity.lindi.vo.contracttemplatefieldoption.LindiContractTemplateFieldOptionQueryVO;
import com.minimalist.common.entity.lindi.vo.contracttemplatefieldoption.LindiContractTemplateFieldOptionVO;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiContractTemplateFieldOptionMapper;
import com.minimalist.lindi.service.LindiContractTemplateFieldOptionService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标平台_合同模板字段选项表 服务层实现。
 *
 * @author 11987
 * @since 2025-05-31
 */
@Service
public class LindiContractTemplateFieldOptionServiceImpl extends
        ServiceImpl<LindiContractTemplateFieldOptionMapper
                , LindiContractTemplateFieldOption> implements
        LindiContractTemplateFieldOptionService{

    @Autowired
    private LindiContractTemplateFieldOptionMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addContractTemplateFieldOption(
            LindiContractTemplateFieldOptionVO contractTemplateFieldOptionVO){
        LindiContractTemplateFieldOption option = BeanUtil.copyProperties(
                contractTemplateFieldOptionVO, LindiContractTemplateFieldOption.class);
        option.setContractTemplateFieldOptionId(UnqIdUtil.uniqueId());
        int insert = mapper.insert(option, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                LindiContractTemplateFieldOptionEnum.ErrorMsgEnum
                        .ADD_CONTRACT_TEMPLATE_FIELD_OPTION_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContractTemplateFieldOptionByContractTemplateFieldOptionId(
            Long contractTemplateFieldOptionId){
        LindiContractTemplateFieldOption option = mapper
                .selectContractTemplateFieldOptionByContractTemplateFieldOptionId(
                        contractTemplateFieldOptionId);
        Assert.notNull(option, () -> new BusinessException(
                LindiContractTemplateFieldOptionEnum.ErrorMsgEnum.CONTRACT_TEMPLATE_FIELD_OPTION_NOT_FOUND.getDesc()));
        int delete = mapper.delete(option);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                LindiContractTemplateFieldOptionEnum.ErrorMsgEnum.DELETE_CONTRACT_TEMPLATE_FIELD_OPTION_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContractTemplateFieldOptionByContractTemplateFieldOptionId(LindiContractTemplateFieldOptionVO contractTemplateFieldOptionVO){
        LindiContractTemplateFieldOption option = mapper
                .selectContractTemplateFieldOptionByContractTemplateFieldOptionId(
                        contractTemplateFieldOptionVO.getContractTemplateFieldOptionId());
        Assert.notNull(option, () -> new BusinessException(
                LindiContractTemplateFieldOptionEnum.ErrorMsgEnum
                        .CONTRACT_TEMPLATE_FIELD_OPTION_NOT_FOUND.getDesc()));
        BeanUtil.copyProperties(contractTemplateFieldOptionVO, option
                , CopyOptions.create().ignoreNullValue());
        int update = mapper.update(option);
        Assert.isTrue(update == 1, () -> new BusinessException(
                LindiContractTemplateFieldOptionEnum.ErrorMsgEnum
                        .UPDATE_CONTRACT_TEMPLATE_FIELD_OPTION_ERROR.getDesc()));
    }

    @Override
    public PageResp<LindiContractTemplateFieldOptionVO>
    getPageContractTemplateFieldOptionList(
            LindiContractTemplateFieldOptionQueryVO queryVO){
        // 分页查询
        Page<LindiContractTemplateFieldOption> optionPage = mapper
                .selectPageContractTemplateFieldOptionList(queryVO);
        // 数据转换
        List<LindiContractTemplateFieldOptionVO> voList = BeanUtil.copyToList(
                optionPage.getRecords(), LindiContractTemplateFieldOptionVO.class);
        return new PageResp<>(voList, optionPage.getTotalRow());
    }

    @Override
    public LindiContractTemplateFieldOptionVO
    getContractTemplateFieldOptionByContractTemplateFieldOptionId(
            Long contractTemplateFieldOptionId){
        LindiContractTemplateFieldOption option = mapper
                .selectContractTemplateFieldOptionByContractTemplateFieldOptionId(
                        contractTemplateFieldOptionId);
        Assert.notNull(option, () -> new BusinessException(
                LindiContractTemplateFieldOptionEnum.ErrorMsgEnum
                        .CONTRACT_TEMPLATE_FIELD_OPTION_NOT_FOUND.getDesc()));
        return BeanUtil.copyProperties(option, LindiContractTemplateFieldOptionVO.class);
    }
}
